<template>
    <div @click.stop :id="'func-tag-' + funcId" class="func-tag-container">
        <div class="func-tag">
            <div class="func-info">
                <i :class="`func-icon ${DEFAULT_FUNCTION_ICON}`"></i>
                <div class="func-content">
                    <div class="func-header">
                        <span :title="funcName" class="func-name">{{ funcName }}</span>
                        <span class="func-tools">
                            <i class="icon-hover" title="添加快速访问" v-show="!isQuickAccess" v-html="quickAccessSvg"
                                @click.stop="addQuickAccess"></i>
                            <i class="icon-hover" v-html="quickAccessSvg_fixed" title="取消快速访问" v-show="isQuickAccess"
                                @click.stop="removeQuickAccess"></i>
                            <i class="icon-hover-yellow" v-show="!isFavorited" title="添加我的收藏" v-html="favorite_line"
                                @click.stop="addFavorite"></i>
                            <i class="icon-hover-yellow favorited" v-show="isFavorited" title="取消我的收藏"
                                v-html="favorite_file" @click.stop="removeFavorite"></i>
                        </span>
                    </div>
                    <div class="func-stats">
                        <span class="access-time">最近访问 <span class="stat-value">{{ lastVisitTime }}</span></span>
                        <span class="access-count">访问次数 <span class="stat-value">{{ visitCount }}</span></span>
                    </div>
                </div>
            </div>
            <div class="func-actions">
                <span @click="openInCurrentTab" class="action-item current-tab" title="在当前标签页打开">
                    <i class="icon-hover action-icon" v-html="curTabOpenSvg"></i>
                    <span class="action-text">打开</span>
                </span>
                <span @click="handleOpenInNewTab" class="action-item" title="在新标签页打开">
                    <i class="icon-hover action-icon" v-html="newTabOpenSvg"></i>
                    <span class="action-text">新Tab页打开</span>
                </span>
            </div>
        </div>
        <div class="func-description"></div>
    </div>
</template>

<script setup>
import { ref } from 'vue'
import { quickAccessSvg_fixed, curTabOpenSvg, quickAccessSvg, newTabOpenSvg, favorite_line, favorite_file, DEFAULT_FUNCTION_ICON } from '@/constants/svg.js'
import { navigateToMenu, navigateInNewTab } from '@/service/funcMenuService'

const emit = defineEmits(['close-popover'])

const props = defineProps({
    funcId: {
        type: String,
        default: ''
    },
    funcName: {
        type: String,
        default: ''
    }
})

const isQuickAccess = ref(false)
const isFavorited = ref(false)
const lastVisitTime = ref('')
const visitCount = ref(0)

const removeFavorite = () => {
    isFavorited.value = false
}

const addFavorite = () => {
    isFavorited.value = true
}

const addQuickAccess = () => {
    isQuickAccess.value = true
}

const removeQuickAccess = () => {
    isQuickAccess.value = false
}

const openInCurrentTab = () => {
    navigateToMenu(props.funcId)
    // 抛个事件 将当前打开的关闭掉
    emit('close-popover')
}

const handleOpenInNewTab = () => {
    navigateInNewTab(props.funcId)
}

</script>

<style lang="scss" scoped>
.func-tag-container {
    // 容器样式
}

.func-tag {
    padding: 24px 24px 8px 22px;
}

.func-info {
    display: flex;
    align-items: flex-start;
}

.func-icon {
    font-size: 42px;
    min-height: 42px;
    min-width: 42px;
    display: inline-block;
    color: var(--theme-color);
}

.func-content {
    flex: 1;
    margin-left: 12px;
}

.func-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
}

.func-name {
    color: #333333;
    max-width: 130px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-size: 18px;
    font-weight: bold;
    min-width: 130px;
}

.func-tools {
    font-size: 16px;

    i {
        cursor: pointer;
        width: 14px;
        height: 14px;
        margin-left: 8px;
    }
}

.func-stats {
    display: flex;
    justify-content: space-between;
    padding-top: 12px;
    font-size: 12px;
    color: #727272;
}

.access-time {
    .stat-value {
        color: rgba(0, 0, 0, 0.9);
    }
}

.access-count {
    .stat-value {
        color: rgba(0, 0, 0, 0.9);
    }
}

.func-actions {
    padding-top: 24px;
    font-size: 14px;
    color: #727272;
}

.action-item {
    cursor: pointer;
    line-height: 16px;

    &.current-tab {
        padding-right: 7px;
        display: inline-block;
    }

    &:hover {
        color: var(--theme-color);

        .action-text {
            color: var(--theme-color);
        }

        svg {
            fill: var(--theme-color);
        }
    }
}

.action-icon {
    cursor: pointer;
    font-size: 14px;
}

.action-text {
    color: rgba(0, 0, 0, 0.9);
    padding-left: 5px;
}

.func-description {
    padding: 12px 24px 0px 24px;
}

// 图标悬停效果
.icon-hover {
    &:hover {
        color: var(--theme-color);
    }
}

.icon-hover-yellow {
    margin-left: 12px;

    &:hover {
        color: rgb(255, 149, 64);
    }

    &.favorited {
        color: rgb(255, 149, 64);
    }
}

svg:hover {
    fill: var(--theme-color);
}
</style>
